home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #1 / Ham Radio 2000.iso / ham2000 / tcp_ip / tnos / tnos100s / samlib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-21  |  4.4 KB  |  163 lines

  1. #include "config.h"
  2. #ifdef SAMCALLB
  3. /*
  4.  * samlib.c
  5.  *
  6.  * This contains functions to init and access the API contained
  7.  * in resident program SAMAPI.EXE.
  8.  *
  9.  * SAMAPI.EXE is a resident program that provides an application program
  10.  * interface to the SAM Amateur Radio Callsign Database.
  11.  *
  12.  * Three functions in here:
  13.  *  LocateSam     - determines SAMAPI is loaded
  14.  *  CallSam       - call the API, passing a command parameter block and
  15.  *                  a buffer for result/response.
  16.  *  SetCountrySam - set the country for search, 0 = USA, 1 = Canada
  17.  *                  Note that normally automatic for simple call lookup
  18.  *
  19.  * The API is accessed via int 2fh, ah = dynamic muxid (default is 0x99).
  20.  * This is commonly called the "multiplex interrupt" interface.
  21.  * Per convention, int 2fh, ah = muxid, al = 0 is an install check, and
  22.  * al comes back non-zero if the API is installed.  For other API
  23.  * functions:
  24.  *
  25.  *      ah = muxid
  26.  *      al = 1
  27.  *   ds:si = pointer to command block
  28.  *   es:di = pointer to result block
  29.  *
  30.  * No registers are changed by SAMAPI (except al when input al == 0)
  31.  *
  32.  * See samapi.h for details on the command/response data structures,
  33.  * command codes, error codes, and so on.
  34.  *
  35.  * LocateSam looks for an environment string: SAMAPI=number.
  36.  * If found, "number" becomes the muxid.  SAMAPI.EXE does the same thing
  37.  * when it loads so muxid can be changed when conflicts arise.
  38.  */
  39.  
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. #include <string.h>
  43. #include <dos.h>
  44.  
  45. #include "samapi.h"
  46.  
  47.  
  48. int SamMuxid = DEFAULT_SAMMUX;
  49. int SamCountry = 0;                /* 0 == USA, 1 == Canada */
  50.  
  51. /**************************
  52.  * LocateSam
  53.  **************************
  54.  *
  55.  * Finds the resident SAMAPI code.  This MUST be called before
  56.  * other functions are used!.  It checks for the environment
  57.  * string SAMAPI=number to see if there is an override for the
  58.  * muxid (SAMAPI.EXE does the same thing).
  59.  *
  60.  * returns: 0 if resident code found and can be used, -1 if not
  61.  */
  62.  
  63. int LocateSam(void)
  64. {
  65.     int v;
  66.     char *p;
  67.     static union REGS r;
  68.  
  69.     p = getenv("SAMAPI");
  70.     if (p != NULL)
  71.     {
  72.         v = atoi(p);
  73.         if (v > 0 && v < 255)
  74.             SamMuxid = v;
  75.     }
  76.     r.h.al = 0;
  77.     r.h.ah = SamMuxid;
  78.     int86(0x2f, &r, &r);
  79.     return (r.h.al == 1) ? 0 : -1;
  80. }
  81.  
  82. /**************************
  83.  * CallSam
  84.  **************************
  85.  *
  86.  * Call the resident SAMAPI code.
  87.  *
  88.  * This expects a filled-in (all but header) command buffer
  89.  * and a to-be-filled-in response buffer.  cmd is stuffed into
  90.  * cmdbuf and the resident code is called (via int 0x2f).
  91.  *
  92.  * returns: error code from response buffer header (0 usually
  93.  *          means no error)
  94.  */
  95.  
  96. int CallSam(int cmd, void far *cmdbuf, void far *rspbuf)
  97. {
  98.     static union REGS r;
  99.     static struct SREGS sr;
  100.     int i;
  101.  
  102.     ((chdr_t far *) cmdbuf)->cmd = cmd;
  103.  
  104.     /* make reserved fields 0 for future compatability. */
  105.     for (i = 0; i < 2; i++)
  106.         ((chdr_t far *) cmdbuf)->fill[i] = 0;
  107.  
  108.     /* select which database */
  109.     ((chdr_t far *) cmdbuf)->country = SamCountry;
  110.  
  111.     r.x.si = FP_OFF(cmdbuf);        /* ds:si is ptr to command */
  112.     sr.ds = FP_SEG(cmdbuf);
  113.     r.x.di = FP_OFF(rspbuf);        /* es:di is ptr to result buf */
  114.     sr.es = FP_SEG(rspbuf);
  115.     r.h.al = 1;
  116.     r.h.ah = SamMuxid;                /* ax = (muxid << 8) + 1 */
  117.     int86x(0x2f, &r, &r, &sr);        /* do the int 0x2f */
  118.     return ((rhdr_t far *) rspbuf)->err;    /* return error code in rsp buf */
  119. }
  120.  
  121. /**************************
  122.  * SetCountrySam
  123.  **************************
  124.  *
  125.  * Selects the country for subsequent operations.  i.e., selects database
  126.  *
  127.  *  0 = USA
  128.  *  1 = Canada
  129.  *
  130.  * returns 0 if success, else !0 if error.  Possible errors are
  131.  * illegal country code or Canada database not installed.
  132.  *
  133.  * Note that for lookup by call, country selection is automatic unless
  134.  * samapi was started with /noauto option.  However, you need to select
  135.  * country to access database by record number, name record number,
  136.  * qth searches, etc.
  137.  */
  138.  
  139. int SetCountrySam(int country)
  140. {
  141.     chdr_t cmd;
  142.     rspnumrecs_t resp;
  143.     int old;
  144.  
  145.     if (LocateSam())
  146.         return -1;
  147.  
  148.     old = SamCountry;        /* save in case can't switch */
  149.     SamCountry = country;
  150.  
  151.     /* get Number of records for new country ...
  152.        * if no error, must have been ok to switch
  153.        */
  154.  
  155.     if (CallSam(SamGetNumRecs, &cmd, &resp))
  156.     {
  157.         SamCountry = old;        /* error, so don't switch */
  158.         return -1;
  159.     }
  160.     return 0;
  161. }
  162. #endif
  163.